home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / fontutil.6 / fontutil / fontutils-0.6 / pk / pk_input.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-13  |  21.8 KB  |  730 lines

  1. /* pk_input.c: read from any number of PK files.  The basic idea is to
  2.    read the entire file into memory the first time the client asks for a
  3.    character.  Since PK files tend to be quite small, this does not use
  4.    an enormous amount of memory.  But we only bother to unpack a
  5.    character's bitmap when the client asks for that particular
  6.    character.
  7.  
  8. Copyright (C) 1992 Free Software Foundation, Inc.
  9.  
  10. This program is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation; either version 2, or (at your option)
  13. any later version.
  14.  
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. GNU General Public License for more details.
  19.  
  20. You should have received a copy of the GNU General Public License
  21. along with this program; if not, write to the Free Software
  22. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  23.  
  24. #include "config.h"
  25.  
  26. #include "file-input.h"
  27. #include "list.h"
  28. #include "pk.h"
  29. #include "pk_opcodes.h"
  30.  
  31.  
  32. /* The `packed_char_type' structure holds the character description,
  33.    taken directly from the input file, before it is unpacked into a
  34.    bitmap.  This type is only used as part of the `internal_font_type'
  35.    structure, above.  (In contrast, the `pk_char_type' structure holds
  36.    the unpacked information, in the form convenient for clients.)  */
  37.  
  38. typedef struct
  39. {
  40.   unsigned dyn_f : 4;
  41.   unsigned black_first : 1;
  42.   unsigned parameter_length : 3;
  43.   unsigned packet_length;
  44.   one_byte *data;
  45. } packed_char_type;
  46.  
  47. /* This is the number of one-nybble run counts.  It is at most 14.  */
  48. #define PACKED_DYN_F(c)  ((c).dyn_f)
  49.  
  50. /* This says whether the first run count is black or white.  */
  51. #define PACKED_BLACK_FIRST(c) ((c).black_first)
  52.  
  53. /* This says how long the size parameters in the character preamble are.
  54.    It is either 1, 2, or 4.  Either 1 or 2 means we're using a ``short
  55.    format''; 4 means we're using the ``long format''.  */
  56. #define PACKED_PARAM_LENGTH(c)  ((c).parameter_length)
  57. #define LONG_FORMAT(c)  (PACKED_PARAM_LENGTH (c) == 4)
  58.  
  59. /* This is the length of the packed character data, in bytes.  */
  60. #define PACKED_DATA_LENGTH(c)  ((c).packet_length)
  61.  
  62. /* And the packed data, as an array of `packet_length' bytes.  */
  63. #define PACKED_DATA(c)  ((c).data)
  64.  
  65.  
  66. /* We save an instance of this structure for each font we are asked to
  67.    open.  */
  68.  
  69. typedef struct
  70. {
  71.   FILE *file;
  72.   pk_char_type *chars[MAX_CHARCODE + 1];
  73.   packed_char_type *packed_chars[MAX_CHARCODE + 1];
  74.   boolean chars_read;
  75. } internal_font_type;
  76.  
  77. /* The file pointer for the font F.  */
  78. #define INTERNAL_FILE(f)  ((f).file)
  79.  
  80. /* This pointer is null if the character C in the internal font F has
  81.    not yet been unpacked.  */
  82. #define INTERNAL_PK_CHAR(f, c)  ((f).chars[c])
  83.  
  84. /* This pointer is null if the character C does not exist in F.  */
  85. #define INTERNAL_PACKED_CHAR(f, c)  ((f).packed_chars[c])
  86.  
  87. /* This remembers whether we have read the character info from the file.  */
  88. #define INTERNAL_CHARS_READ(f)  ((f).chars_read)
  89.  
  90.  
  91. /* These macros get at the members of the packed character numbered C,
  92.    in the internal font F.  */
  93.  
  94. #define INTERNAL_PACKED_DYN_F(f, c)                    \
  95.   PACKED_DYN_F (*INTERNAL_PACKED_CHAR (f, c))
  96. #define INTERNAL_PACKED_BLACK_FIRST(f, c)                \
  97.   PACKED_BLACK_FIRST (*INTERNAL_PACKED_CHAR (f, c))
  98. #define INTERNAL_PACKED_PARAM_LENGTH(f, c)                \
  99.   PACKED_PARAM_LENGTH (*INTERNAL_PACKED_CHAR (f, c))
  100. #define INTERNAL_PACKED_DATA_LENGTH(f, c)                \
  101.   PACKED_DATA_LENGTH (*INTERNAL_PACKED_CHAR (f, c))
  102. #define INTERNAL_PACKED_DATA(f, c)                    \
  103.   PACKED_DATA (*INTERNAL_PACKED_CHAR (f, c))
  104.  
  105. /* File I/O.  */
  106. static FILE *pk_input_file;      /* The file we're currently reading.  */
  107. static string current_filename;  /* Its pathname.  */
  108.  
  109. /* Low-level input.  These macros call the corresponding routines in
  110.    kbase, using the static variables for the input file and filename.  */
  111.  
  112. #define PK_MATCH_BYTE(expected) \
  113.   match_byte (expected, pk_input_file, current_filename)
  114. #define PK_GET_BYTE() get_byte (pk_input_file, current_filename)
  115. #define PK_GET_TWO() get_two (pk_input_file, current_filename)
  116. #define PK_GET_FOUR() get_four (pk_input_file, current_filename)
  117. #define PK_GET_N_BYTES(n) get_n_bytes (n, pk_input_file, current_filename)
  118.  
  119. static four_bytes pk_get_n_byte_value (unsigned);
  120. static string pk_get_string (unsigned);
  121.  
  122. static void get_all_pk_chars (internal_font_type *);
  123. static void get_one_pk_char (internal_font_type *, one_byte);
  124.  
  125. /* Memory ``I/O''.  */
  126. static pk_char_type *unpack (packed_char_type);
  127. static void get_bitmap (pk_char_type *);
  128. static void get_packed_bitmap (pk_char_type *, packed_char_type);
  129. static unsigned get_run_count (one_byte, unsigned *);
  130.  
  131. static one_byte *pk_input_data = NULL;  /* Where we are reading from.  */
  132. static boolean do_upper_nybble = true;  /* Which nybble we're reading.  */
  133.  
  134. static one_byte data_get_bit (void);
  135. static one_byte data_get_nybble (void);
  136. static four_bytes data_get_three (void);
  137. static four_bytes data_get_four (void);
  138. static four_bytes data_get_n_byte_value (one_byte);
  139. static signed_4_bytes data_get_signed_n_byte_value (one_byte);
  140.  
  141.  
  142. /* Handle multiple fonts.  */
  143. static internal_font_type *find_internal_font (string);
  144. static void save_internal_font (string, FILE *);
  145. static void delete_internal_font (string);
  146.  
  147. /* Prepare for reading FILENAME.  Return false if it can't be opened. 
  148.    We allow the same file to be opened twice; I'm not sure if that's a
  149.    good idea or not, but it doesn't seem to matter much.  */
  150.  
  151. boolean
  152. pk_open_input_file (string filename)
  153. {
  154.   FILE *f = fopen (filename, "r");
  155.   
  156.   if (f == NULL) return false;
  157.   
  158.   save_internal_font (filename, f);
  159.   return true;
  160. }
  161.  
  162.  
  163. /* Close the file associated with FILENAME, if we have it open.  If it's
  164.    not open, the caller must have made a mistake, so
  165.    `delete_internal_font' gives a fatal error.  */
  166.  
  167. void
  168. pk_close_input_file (string filename)
  169. {
  170.   delete_internal_font (filename);
  171. }
  172.  
  173. /* The PK preamble contains the fontwide information we are supposed to
  174.    return.  If FONT_NAME isn't open for reading, we give a fatal error.  */
  175.  
  176. pk_preamble_type
  177. pk_get_preamble (string filename)
  178. {
  179.   pk_preamble_type p;
  180.   unsigned comment_length;
  181.   internal_font_type *f = find_internal_font (filename);
  182.   
  183.   assert (INTERNAL_FILE (*f) != NULL);
  184.   pk_input_file = INTERNAL_FILE (*f);
  185.   current_filename = filename;
  186.  
  187.   /* The preamble is at the beginning of the file, so move there.  */
  188.   xfseek (pk_input_file, 0, SEEK_SET, filename);
  189.   
  190.   PK_MATCH_BYTE (PK_PRE);
  191.   PK_MATCH_BYTE (PK_ID);
  192.   comment_length = PK_GET_BYTE ();
  193.   PK_COMMENT (p) = pk_get_string (comment_length);
  194.   PK_DESIGN_SIZE (p) = PK_GET_FOUR ();
  195.   PK_CHECKSUM (p) = PK_GET_FOUR ();
  196.   PK_H_RESOLUTION (p) = PK_GET_FOUR ();
  197.   PK_V_RESOLUTION (p) = PK_GET_FOUR ();
  198.  
  199.   /* We read (but not unpack) the entire file.  */
  200.   get_all_pk_chars (f);
  201.   
  202.   return p;
  203. }
  204.  
  205. /* Return the information for the character CODE in the font FILENAME,
  206.    or null if the character doesn't exist in that font.  */
  207.  
  208. pk_char_type *
  209. pk_get_char (one_byte code, string filename)
  210. {
  211.   internal_font_type *f = find_internal_font (filename);
  212.   
  213.   assert (INTERNAL_FILE (*f) != NULL);
  214.   pk_input_file = INTERNAL_FILE (*f);  
  215.   current_filename = filename;
  216.  
  217.   if (!INTERNAL_CHARS_READ (*f))
  218.     /* This routine sets the character info in F (among other things).  */
  219.     get_all_pk_chars (f);
  220.  
  221.   /* The PK_CHAR is null until we unpack the bitmap.  If the character
  222.      is not in the font at all, then the PACKED_CHAR is null.   */
  223.   if (INTERNAL_PK_CHAR (*f, code) != NULL)
  224.     /* We've already unpacked this character.  */
  225.     return INTERNAL_PK_CHAR (*f, code);
  226.  
  227.   else if (INTERNAL_PACKED_CHAR (*f, code) == NULL)
  228.     /* The character is not in the font.  */
  229.     return NULL;
  230.  
  231.   else
  232.     { /* We need to unpack the character.  */
  233.       INTERNAL_PK_CHAR (*f, code) = unpack (*INTERNAL_PACKED_CHAR (*f, code));
  234.       PK_CHARCODE (*INTERNAL_PK_CHAR (*f, code)) = code;
  235.       return INTERNAL_PK_CHAR (*f, code);
  236.     }
  237. }
  238.  
  239.  
  240. /* This routine reads the contents of the entire `pk_input_file' (which
  241.    should be set before calling it).  The results go into F.  */
  242.  
  243. static void
  244. get_all_pk_chars (internal_font_type *f)
  245. {
  246.   while (true)  /* The loop exits when we see PK_POST.  */
  247.     {
  248.       one_byte command = PK_GET_BYTE ();
  249.       
  250.       if (command < PK_XXX1)
  251.         get_one_pk_char (f, command);        
  252.  
  253.       else if (PK_XXX1 <= command && command <= PK_XXX4)
  254.         /* The length of the string is given by the next 1--4 bytes,
  255.            according to the opcode.  We just ignore all specials.  */
  256.         (void) PK_GET_N_BYTES (pk_get_n_byte_value (command - PK_XXX1 + 1));
  257.       
  258.       else if (command == PK_YYY)
  259.         (void) PK_GET_FOUR ();
  260.       
  261.       else if (command == PK_POST)
  262.         break; 
  263.  
  264.       else if (command == PK_NO_OP)
  265.         /* do nothing */;
  266.  
  267.       else
  268.         FATAL1 ("Undefined command byte %u in PK file", command);
  269.     }
  270.   
  271.   INTERNAL_CHARS_READ (*f) = true;
  272. }
  273.  
  274.  
  275. /* Read a single character, starting at the current position of
  276.    `pk_input_file'.  The results go into F.  */
  277.  
  278. static void
  279. get_one_pk_char (internal_font_type *f, one_byte flag)
  280. {
  281.   unsigned parameter_length, packet_length;
  282.   int charcode;
  283.   one_byte dyn_f = flag >> 4;
  284.  
  285.   if (dyn_f == 15)
  286.     FATAL1 ("Unrecognized command %u", flag);
  287.  
  288.   if (!(flag & 4))
  289.     { /* Short format.  */
  290.       parameter_length = 1;
  291.       packet_length = ((flag & 3) << 8) + PK_GET_BYTE ();
  292.       charcode = PK_GET_BYTE ();
  293.     }
  294.   else if (!(flag & 1))
  295.     { /* Extended short format.  */
  296.       parameter_length = 2;
  297.       packet_length = ((flag & 3) << 16) + PK_GET_TWO ();
  298.       charcode = PK_GET_BYTE ();
  299.     }
  300.   else
  301.     { /* Long format.  */
  302.       parameter_length = 4;
  303.       packet_length = PK_GET_FOUR ();
  304.       charcode = PK_GET_FOUR ();
  305.       if (charcode > MAX_CHARCODE || charcode < 0)
  306.         /* Someone is trying to use a font with character codes that are
  307.            out of our range.  */
  308.         FATAL1 ("Character code %d out of range 0..255 in PK file (sorry)",
  309.                 charcode);
  310.     }
  311.  
  312.   INTERNAL_PACKED_CHAR (*f, charcode) = xmalloc (sizeof (packed_char_type));
  313.   INTERNAL_PACKED_DYN_F (*f, charcode) = dyn_f;
  314.   INTERNAL_PACKED_BLACK_FIRST (*f, charcode) = (flag & 8) != 0;
  315.   INTERNAL_PACKED_PARAM_LENGTH (*f, charcode) = parameter_length;
  316.   INTERNAL_PACKED_DATA_LENGTH (*f, charcode) = packet_length;
  317.   INTERNAL_PACKED_DATA (*f, charcode)
  318.     = (one_byte *) PK_GET_N_BYTES (packet_length);
  319. }
  320.  
  321.  
  322. /* This routine unpacks a character C into the raster image.  All the
  323.    fields but one in the `pk_char_type' structure are filled in.  The
  324.    exception is the character code, which our caller does.  */
  325.  
  326. static pk_char_type *
  327. unpack (packed_char_type c)
  328. {
  329.   dimensions_type d;
  330.   signed_4_bytes h_offset, v_offset;
  331.   pk_char_type *pk_char = xmalloc (sizeof (pk_char_type));
  332.  
  333.   /* So it doesn't have to be a parameter to all the routines.  */
  334.   pk_input_data = PACKED_DATA (c);
  335.   do_upper_nybble = true; /* The data always starts on a byte boundary.  */
  336.  
  337.   /* The TFM width is three bytes in the short formats, four in the
  338.      long format.  */
  339.   PK_TFM_WIDTH (*pk_char)
  340.     = LONG_FORMAT (c) ? data_get_four () : data_get_three ();
  341.  
  342.   /* The horizontal escapement is the next PARAMETER_LENGTH bytes.  In
  343.      addition, if we're using the long format, it is in pixels
  344.      multiplied by 2^16, and we want to unscale it.  */
  345.   PK_H_ESCAPEMENT (*pk_char)
  346.     = data_get_n_byte_value (PACKED_PARAM_LENGTH (c));
  347.   if (LONG_FORMAT (c))
  348.     {
  349.       PK_H_ESCAPEMENT (*pk_char) >>= 16;/* Don't bother to round.  */
  350.       (void) data_get_four ();        /* Ignore the vertical escapement.  */
  351.     }
  352.  
  353.   /* Next comes the size of the bitmap.  */
  354.   DIMENSIONS_WIDTH (d) = data_get_n_byte_value (PACKED_PARAM_LENGTH (c));
  355.   DIMENSIONS_HEIGHT (d) = data_get_n_byte_value (PACKED_PARAM_LENGTH (c));
  356.   PK_BITMAP (*pk_char) = new_bitmap (d);
  357.   
  358.   /* The last two values in the character preamble are the offsets from
  359.      the upper left pixel to the reference pixel.  */
  360.   h_offset = data_get_signed_n_byte_value (PACKED_PARAM_LENGTH (c));
  361.   v_offset = data_get_signed_n_byte_value (PACKED_PARAM_LENGTH (c));
  362.   MIN_COL (PK_CHAR_BB (*pk_char)) = -h_offset;
  363.   MAX_COL (PK_CHAR_BB (*pk_char)) = -h_offset + DIMENSIONS_WIDTH (d);
  364.   MIN_ROW (PK_CHAR_BB (*pk_char)) = v_offset + 1 - DIMENSIONS_HEIGHT (d);
  365.   MAX_ROW (PK_CHAR_BB (*pk_char)) = v_offset;
  366.  
  367.   if (PACKED_DYN_F (c) == PK_BITMAP_FLAG)
  368.     get_bitmap (pk_char);
  369.   else
  370.     get_packed_bitmap (pk_char, c);
  371.  
  372.   return pk_char;
  373. }
  374.  
  375.  
  376. /* Turn the packed bitmap in the character C into a real bitmap, in
  377.    PK_CHAR.  The bitmap storage and sizes in PK_CHAR must already be
  378.    set.  The packed data should encode exactly as many bits as are in
  379.    the bitmap.  */
  380.  
  381. static void
  382. get_packed_bitmap (pk_char_type *pk_char, packed_char_type c)
  383. {
  384.   bitmap_type b = PK_BITMAP (*pk_char);
  385.   unsigned row = 0;
  386.   unsigned col = 0;
  387.   unsigned row_repeat_count = 0;
  388.   one_byte color = PACKED_BLACK_FIRST (c);
  389.  
  390.   while (row < BITMAP_HEIGHT (b))
  391.     {
  392.       unsigned i;
  393.       unsigned cmd_repeat_count;
  394.       unsigned run_count = get_run_count (PACKED_DYN_F (c), &cmd_repeat_count);
  395.  
  396.       if (cmd_repeat_count != 0)
  397.         {
  398.           if (row_repeat_count != 0)
  399.             FATAL2 ("Two repeat counts (%u and %u) in PK row",
  400.                    row_repeat_count, cmd_repeat_count);
  401.           row_repeat_count = cmd_repeat_count;
  402.         }
  403.  
  404.       /* Start painting pixels.  */
  405.       for (i = 0; i < run_count; i++)
  406.         {
  407.           BITMAP_PIXEL (b, row, col) = color;
  408.           col++;
  409.           
  410.           /* It is inefficient to test this here.  Sorry.  */
  411.           if (col == BITMAP_WIDTH (b))
  412.             { /* We've finished a row.  If we saw a repeat count
  413.                  somewhere within it, we should duplicate it.  
  414.                  If the PK file is correct, `row' will not increase past
  415.                  the height of the bitmap from repeat counts, but we may
  416.                  as well be safe.  */
  417.               for (row++;
  418.                    row_repeat_count > 0 && row < BITMAP_HEIGHT (b); 
  419.                    row_repeat_count--, row++)
  420.                 for (col = 0; col < BITMAP_WIDTH (b); col++)
  421.                   BITMAP_PIXEL (b, row, col) = BITMAP_PIXEL (b, row - 1, col);
  422.  
  423.               /* Start painting pixels from the run count at the
  424.                  beginning of the next row.  */
  425.               col = 0;
  426.             }
  427.         }
  428.  
  429.       color = !color;
  430.     }
  431.  
  432.     if (col != 0)
  433.       FATAL ("The PK bitmap ended in a strange place");
  434. }
  435.  
  436.  
  437. /* This routine is called `pk_packed_num' in the PK documentation, but
  438.    as far as the caller is concerned, it doesn't return a packed number,
  439.    it returns a run count.  Due to the definition of packed data, it is
  440.    impractical to return repeat counts in any other way except together
  441.    with a run count.  */
  442.  
  443. static unsigned
  444. get_run_count (one_byte dyn_f, unsigned *repeat_count)
  445. {
  446.   unsigned n = data_get_nybble ();
  447.   
  448.   *repeat_count = 0;
  449.   
  450.   if (n == LONG_RUN_COUNT_FLAG)
  451.     { /* Get a three nybble (or more) value, represented as some number
  452.          of zeros, followed by that many hexadecimal values.  The
  453.          largest such number that we'll be asked to find will fit in 
  454.          32 bits.  */
  455.       unsigned zero_count = 0;
  456.       do
  457.         {
  458.           n = data_get_nybble ();
  459.           zero_count++;
  460.         }
  461.       while (n == 0);
  462.       
  463.       for ( ; zero_count > 0; zero_count --)
  464.         n = (n << 4) + data_get_nybble ();
  465.       
  466.       /* The smallest three-nybble value is one more than the largest
  467.          two nybble value.  */
  468.       return n - 15 + ((13 - dyn_f) << 4) + dyn_f;
  469.     }
  470.  
  471.   else if (n <= dyn_f)
  472.     /* The value we just read is itself the number.  */
  473.     return n;
  474.  
  475.   else if (n < REPEAT_COUNT_FLAG)
  476.     /* Get a two-nybble value.  */
  477.     return ((n - dyn_f - 1) << 4) + data_get_nybble () + dyn_f + 1;
  478.   
  479.   else if (n == REPEAT_COUNT_FLAG)
  480.     /* If the PK file is valid, this recursion will go only one level
  481.        deep, because a repeat count should never be followed by another
  482.        repeat count.  In other words, the next nybble must be a ``run
  483.        count'' in some form.  (This is why this routine is called
  484.        `pk_packed_num' in other programs.)  */
  485.     *repeat_count = get_run_count (dyn_f, &n);
  486.  
  487.   else
  488.     *repeat_count = 1;
  489.   
  490.   /* We only get here if we saw a repeat count.  Now we have to get the
  491.      run count which should be next; there won't be any repeat count.  */
  492.   return get_run_count (dyn_f, &n);
  493. }
  494.  
  495.  
  496. /* The PK format also allows for a straight bitmap representation in the
  497.    file, one pixel per bit.  Here we put each pixel into a byte.  */
  498.  
  499. static void
  500. get_bitmap (pk_char_type *pk_char)
  501. {
  502.   unsigned row;
  503.   unsigned col;
  504.   one_byte extra_bits;
  505.   bitmap_type b = PK_BITMAP (*pk_char);
  506.   
  507.   for (row = 0; row < BITMAP_HEIGHT (b); row++)
  508.     for (col = 0; col < BITMAP_WIDTH (b); col++)
  509.       BITMAP_PIXEL (b, row, col) = data_get_bit ();
  510.   
  511.   /* If the bitmap did not completely fill the last byte, we should read
  512.      and ignore the remaining bits.  */
  513.   extra_bits = 8 - (row * col) % 8;
  514.   while (extra_bits-- > 0)
  515.     (void) data_get_bit ();
  516. }
  517.  
  518. /* Low-level routines that read from the disk file.  */
  519.  
  520. static string
  521. pk_get_string (unsigned length)
  522. {
  523.   string s;
  524.   
  525.   if (length == 0) return "";
  526.   
  527.   s = PK_GET_N_BYTES (length);
  528.   s = xrealloc (s, length + 1);
  529.   s[length] = 0;
  530.   return s;
  531. }
  532.  
  533.  
  534. /* N must be <= 4.  */
  535.  
  536. static four_bytes
  537. pk_get_n_byte_value (unsigned n)
  538. {
  539.   four_bytes v = 0;
  540.   
  541.   for ( ; n > 0; n--)
  542.     v |= PK_GET_BYTE () << ((n - 1) * 8);
  543.  
  544.   return v;
  545. }
  546.  
  547. /* These routines do not read from a disk file.  Instead, they read from
  548.    memory (where we have put each character's definition, directly from
  549.    the file.)  They use the semi-global `pk_input_data'.  */
  550.  
  551.  
  552. /* This routine is used when the character is defined as a bitmap,
  553.    instead of run-encoded.  */
  554.  
  555. static one_byte
  556. data_get_bit ()
  557. {
  558.   static one_byte mask = 0;
  559.   static one_byte n;
  560.   one_byte value;
  561.   
  562.   if (mask == 0)
  563.     {
  564.       n = data_get_nybble ();
  565.       mask = 0x8;
  566.     }
  567.   
  568.   value = (n & mask) != 0;
  569.   mask >>= 1;
  570.   
  571.   return value;
  572. }
  573.  
  574.  
  575. /* Since the PK format is based on nybbles, this routine is by far the
  576.    most frequently called.  */
  577.  
  578. static one_byte
  579. data_get_nybble ()
  580. {
  581.   do_upper_nybble = !do_upper_nybble;
  582.  
  583.   /* Double inversion gets us back the original value.  After we have
  584.      done the lower nybble, we advance to the next byte.  */
  585.   return !do_upper_nybble ? *pk_input_data >> 4 : *pk_input_data++ & 0xf;
  586. }
  587.  
  588.  
  589. /* We assume (correctly) that we are not in the middle of a byte
  590.    when any of the following routines are called.  */
  591.  
  592. static four_bytes
  593. data_get_three ()
  594. {
  595.   return data_get_n_byte_value (3);
  596. }
  597.  
  598.  
  599. static four_bytes
  600. data_get_four ()
  601. {
  602.   return data_get_n_byte_value (4);
  603. }
  604.  
  605.  
  606. static four_bytes
  607. data_get_n_byte_value (one_byte n)
  608. {
  609.   four_bytes v = 0;
  610.   
  611.   for ( ; n > 0; n--)
  612.     v |= *pk_input_data++ << ((n - 1) * 8);
  613.  
  614.   return v;
  615. }
  616.  
  617.  
  618. static signed_4_bytes
  619. data_get_signed_n_byte_value (one_byte passed_n)
  620. {
  621.   signed_4_bytes v = 0;
  622.   unsigned n = passed_n;
  623.   
  624.   assert (n <= 4);
  625.  
  626.   for ( ; n > 0; n--)
  627.     v |= (*pk_input_data++ << ((n - 1) * 8));
  628.  
  629.   if (passed_n == 1)
  630.     return (signed_byte) (v & 0xff);
  631.   else if (passed_n == 2)
  632.     return (signed_2_bytes) (v & 0xffff);
  633.   else if (passed_n == 3)
  634.     return (signed_4_bytes) (v & 0xffffff);
  635.   else
  636.     return (signed_4_bytes) v;
  637.  
  638.   return v;
  639. }
  640.  
  641. /* We assign an index number to NAME, and store both NAME and the
  642.    `internal_font_type' that we construct in parallel lists.  The file F
  643.    goes into the `internal_font_list'.  */
  644.  
  645. static list_type font_name_list, internal_font_list;
  646.  
  647. static void
  648. save_internal_font (string name, FILE *f)
  649. {
  650.   unsigned c;
  651.   string *new_name;
  652.   internal_font_type *new_font;
  653.   static boolean first_call = true;
  654.   
  655.   if (first_call)
  656.     { /* We have to construct our lists.  */
  657.       font_name_list = list_init ();
  658.       internal_font_list = list_init ();
  659.       first_call = false;
  660.     }
  661.  
  662.   new_name = (string *) LIST_TAPPEND (&font_name_list, string);
  663.   new_font = (internal_font_type *)
  664.              LIST_TAPPEND (&internal_font_list, internal_font_type);
  665.  
  666.   /* Save the information.  */
  667.   *new_name = xstrdup (name);
  668.   INTERNAL_FILE (*new_font) = f;
  669.   
  670.   /* We also have to initialize other parts of `new_font'.  */
  671.   for (c = 0; c <= MAX_CHARCODE; c++)
  672.     {
  673.       INTERNAL_PK_CHAR (*new_font, c) = NULL;
  674.       INTERNAL_PACKED_CHAR (*new_font, c) = NULL;
  675.     }
  676.   INTERNAL_CHARS_READ (*new_font) = false;
  677. }
  678.  
  679.  
  680. /* Here we return the `internal_font_type' previously stored along with
  681.    NAME.  If NAME hasn't been saved, we give a fatal error.  */
  682.  
  683. static internal_font_type *
  684. find_internal_font (string name)
  685. {
  686.   unsigned e;
  687.  
  688.   for (e = 0; e < LIST_SIZE (font_name_list); e++)
  689.     if (STREQ (*(string *) LIST_ELT (font_name_list, e), name))
  690.       return (internal_font_type *) LIST_ELT (internal_font_list, e);
  691.  
  692.   FATAL1 ("The PK font `%s' hasn't been saved", name);
  693.   return NULL; /* NOTREACHED */
  694. }
  695.  
  696.  
  697. /* Free the memory and file pointer used by the font FILENAME.  We don't
  698.    delete the list element for now, since removing elements from the
  699.    middle of an array is painful.  Instead, we simply set the name in
  700.    `font_name_list' to the empty string, so that the element is useless.
  701.    Yes, this is disgusting.  */
  702.  
  703. static void
  704. delete_internal_font (string filename)
  705. {
  706.   unsigned e;
  707.  
  708.   /* We can't use `find_internal_font', since we need to know the list
  709.      element index, not just the internal font information.  */ 
  710.   for (e = 0; e < LIST_SIZE (font_name_list); e++)
  711.     if (STREQ (*(string *) LIST_ELT (font_name_list, e), filename))
  712.       {
  713.         unsigned c;
  714.         internal_font_type *f = LIST_ELT (internal_font_list, e);
  715.  
  716.     assert (INTERNAL_FILE (*f) != NULL);
  717.     xfclose (INTERNAL_FILE (*f), filename);
  718.     INTERNAL_FILE (*f) = NULL;
  719.  
  720.     if (INTERNAL_CHARS_READ (*f))
  721.       for (c = 0; c <= MAX_CHARCODE; c++)
  722.         if (INTERNAL_PK_CHAR (*f, c) != NULL) 
  723.           free (INTERNAL_PK_CHAR (*f, c));
  724.         
  725.         *(string *) LIST_ELT (font_name_list, e) = "";
  726.         return;
  727.       }
  728.   FATAL1 ("The PK font `%s' hasn't been saved", filename);
  729. }
  730.